package com.sc.nft.controller;

import com.sc.nft.annotation.UserLogin;
import com.sc.nft.controller.request.IdRequest;
import com.sc.nft.controller.request.UserApplicationForWithdrawalReq;
import com.sc.nft.entity.dto.MallActivityShareDetailDTO;
import com.sc.nft.entity.minimall.MallUserWallet;
import com.sc.nft.entity.vo.UserCommissionVO;
import com.sc.nft.entity.vo.WxMiniUserWithdrawalsVO;
import com.sc.nft.entity.vo.mall.MiniMallOrderPreviewVO;
import com.sc.nft.enums.MallWithdrawType;
import com.sc.nft.enums.RedisKeyEnum;
import com.sc.nft.exception.GlobalException;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.pool.UserPool;
import com.sc.nft.service.MallOrderIncomeService;
import com.sc.nft.service.MallUserWalletService;
import com.sc.nft.service.MallUserWithdrawalsRecordService;
import com.sc.nft.sup.PageRequest;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.concurrent.TimeUnit;

/**
 * @Author：LiGen
 * @Package：com.sc.nft.controller
 * @Project：sc_metago
 * @name：WxMiniUserDistributionController
 * @Date：2024/5/21 15:45
 * @Filename：WxMiniUserDistributionController
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/ipMall/wxMiniMall/financialWithdrawal")
@Api(tags = "用户提现模块")
public class WxMiniUserWithdrawalsController {

    private final MallUserWithdrawalsRecordService mallUserWithdrawalsRecordService;
    private final MallOrderIncomeService mallOrderIncomeService;
    private final MallUserWalletService mallUserWalletService;
    private final Redisson redisson;

    @ApiOperation(value = "用户申请提现", notes = "用户申请提现", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/userApplicationForWithdrawal", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result userApplicationForWithdrawal(@Valid @RequestBody UserApplicationForWithdrawalReq userApplicationForWithdrawalReq) throws GlobalException {
        // 小程序用户锁
        RLock userLock = redisson.getLock(RedisKeyEnum.MALL_SMALL_PROGRAM_ORDER_USER_LOCK.getKey() + UserPool.getWxMiniUserId());
        try {
            boolean res = userLock.tryLock(3, 10, TimeUnit.SECONDS);
            if (res) {
                mallUserWithdrawalsRecordService.userApplicationForWithdrawal(userApplicationForWithdrawalReq.getAmount(), UserPool.getWxMiniUser(), MallWithdrawType.WX_WITHDRAW, userApplicationForWithdrawalReq.getActualName());
            } else {
                return Result.fail("当前人数过多，请稍后再试!");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            throw new GlobalRunTimeException("系统超时");
        } finally {
            userLock.unlock();
        }
        return Result.success();
    }


    @ApiOperation(value = "用户佣金详情", notes = "用户佣金详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/detail", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<UserCommissionVO> detail() throws GlobalException {
        BigDecimal OpenCommission = mallOrderIncomeService.getToBeSettledWxMiniUserCommission(UserPool.getWxMiniUserId());
        MallUserWallet mallUserWallet = mallUserWalletService.getMallUserWallet(UserPool.getWxMiniUserId());
        UserCommissionVO userCommissionVO = new UserCommissionVO();
        userCommissionVO.setOpenCommission(OpenCommission);
        userCommissionVO.setBalance(mallUserWallet.getBalance());
        userCommissionVO.setCumulativeWithdrawal(mallUserWallet.getSumWithdraw());
        return Result.success(userCommissionVO);
    }


    @ApiOperation(value = "用户提现记录", notes = "用户提现记录", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/user/record", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<WxMiniUserWithdrawalsVO> userRecord(@RequestBody PageRequest pageRequest) throws GlobalException {
        return PageResult.success(mallUserWithdrawalsRecordService.getUserWithdrawalsRecord(UserPool.getWxMiniUserId(), pageRequest.getPageNo(), pageRequest.getPageSize()));
    }
}
